package cn.edu.ctbu.websql.service;

import cn.edu.ctbu.websql.domain.Course;
import cn.edu.ctbu.websql.repository.CourseRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;

@Service
public class CourseService {

    @Autowired
    private CourseRepository courseRepository;

    //user_list
    public Page<Course> getAll(Pageable pageable){
        return courseRepository.findAll(pageable);
    }
    public List<Course> findAll(){
        return courseRepository.findAll();
    }
    public void delete(Integer id) {
        courseRepository.deleteById(id);
    }
    public Optional<Course> getData(Integer id){
        return courseRepository.findById(id);
    }
    public Course update(Integer id,Course newCourse){
        Optional<Course> courseOptional = courseRepository.findById(id);
        if(courseOptional.isPresent()){

            Course course = courseOptional.get();
            course.setHighn(newCourse.getHighn());
            course.setLineg(newCourse.getLineg());
            course.setPcc(newCourse.getPcc());
            course.setAcademy(newCourse.getAcademy());

            return courseRepository.save(course);
        }
        return null;
    }
    public List<Course> searchData(String no){
        return courseRepository.findAllByNoLike(no+"%");
    }


}
